(function(angular) {
	"use strict";
	angular.module("fs.waf").factory("custPayService", custPayService);
	custPayService.$inject = [ "$q", "ajaxServiceFactory", "txnService" ];
	function custPayService($q, ajaxServiceFactory, txnService) {
		var ajaxServices = ajaxServiceFactory.buildAjaxService({
			getCustPayLogOptions : {
				url : "do/cls/dataList/getCustPayLogOptions",
				method : "get"
			},
			getCustPayUpdateOptions : {
				url : "do/cls/dataList/getCustPayUpdateOptions",
				method : "get"
			},
			getCurrencyOptions : {
				url : "do/cls/dataList/currencies",
				method : "get"
			}
		});

		function initLogDataOptions() {
			var defer = $q.defer();
			ajaxServices.getCustPayLogOptions().then(function(response) {
				defer.resolve(response.data);
			});
			return defer.promise;
		}

		function initDataOptions() {
			var defer = $q.defer();
			ajaxServices.getCustPayUpdateOptions().then(function(response) {
				defer.resolve(response.data);
			});
			return defer.promise;
		}
		
		function getLayout(packageName, packageSuffix, prodType, readonly) {
			return txnService.getLayout(packageName, packageSuffix, readonly, "CUSTPAY", prodType, undefined);
		}
		
		function deleteDynaparm(layoutConfig) {
			if (layoutConfig) {
				if (layoutConfig.formConfigs && layoutConfig.formConfigs.custPayInfo) {
					var gridConfigs = layoutConfig.formConfigs.custPayInfo.gridConfigs;
					txnService.deleteDynaparm(gridConfigs);
				}
			}
			return layoutConfig;
		}
		
		function getCurrencyOptions() {
			var defer = $q.defer();
			ajaxServices.getCurrencyOptions().then(function(response) {
				defer.resolve(response ? response.data : []);
			}, angular.noop);
			return defer.promise;
		}
		
		function getApCommunity(communityId){
			var ajaxServices = ajaxServiceFactory.buildAjaxService({
				getApCommunity : {
					url : "do/cda/apCommunity/getApCommunityByPk",
					method : "get"
				}
			});
			var defer = $q.defer();
			ajaxServices.getApCommunity({"communityId" : communityId}).then(function(response) {
				defer.resolve(response.data);
			});
			return defer.promise;
		}

		return {
			initDataOptions : initDataOptions,
			getLayout : getLayout,
			deleteDynaparm : deleteDynaparm,
			getCurrencyOptions : getCurrencyOptions,
			initLogDataOptions : initLogDataOptions,
			getApCommunity : getApCommunity
		};
	}
})(angular);